<template>
  <section>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="real_name"
          placeholder="用户"
          clearable
          class="handle-input mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-input
          v-model="mobile"
          placeholder="手机号"
          clearable
          class="handle-input w200 mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-date-picker
          v-model="timeRange"
          type="daterange"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="mr10"
        >
        </el-date-picker>
        <el-button type="primary" icon="search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button type="danger" @click="handleReset">清空</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        v-loading="loading"
      >
        <el-table-column prop="real_name" label="用户名"></el-table-column>
        <el-table-column prop="mobile" label="手机号"></el-table-column>
        <el-table-column prop="date" label="日期"></el-table-column>
        <el-table-column prop="user_number" label="总用户数"></el-table-column>
        <el-table-column
          prop="realname_user_number"
          label="实名用户数"
        ></el-table-column>
        <el-table-column
          prop="level1_user_number"
          label="第一级用户"
        ></el-table-column>
        <el-table-column
          prop="level2_user_number"
          label="第二级用户"
        ></el-table-column>
        <el-table-column
          prop="level3_user_number"
          label="第三级用户"
        ></el-table-column>
        <el-table-column
          prop="income_consume_trade_amount"
          label="团队交易额"
        ></el-table-column>
        <el-table-column
          prop="branch_office_income_rate"
          label="额外分润比例"
        ></el-table-column>
        <el-table-column
          prop="branch_office_income"
          label="额外收益"
        ></el-table-column>
      </el-table>
      <div class="pagination" style="text-align:center">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          layout="sizes, prev, pager, next,total"
          :total="pagination.total"
          :current-page.sync="pagination.currentPage"
          :page-size="pagination.pageSize"
          :page-sizes="pagination.pageSizes"
          :hide-on-single-page="true"
        >
        </el-pagination>
      </div>
    </div>
  </section>
</template>

<script>
import { countList } from '@/api/modules/branch'
import moment from 'moment'

export default {
  name: 'branchCount',
  components: {},
  data() {
    return {
      loading: true,
      tableData: [],
      real_name: '',
      mobile: '',
      timeRange: [],
      pagination: {
        total: 0,
        pageSize: 20,
        currentPage: 1,
        pageSizes: [10, 20, 30]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  computed: {
    startTime: function() {
      return this.timeRange ? this.timeRange[0] : null
    },
    endTime: function() {
      return this.timeRange ? this.timeRange[1] : null
    }
  },
  created() {
    this.getData()
  },
  methods: {
    search() {
      this.getData()
    },
    // 分页导航
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.getData()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getData()
    },
    // 获取数据
    getData() {
      this.loading = true
      countList({
        page: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        mobile: this.mobile,
        real_name: this.real_name,
        start_date: this.startTime,
        end_date: this.endTime
      }).then(res => {
        this.loading = false
        this.tableData = res.data.list
        this.tableData.push({
          real_name: <el-tag style="color:#f00">总计</el-tag>,
          mobile: '/',
          date: '/',
          branch_office_income: res.data.pagination.count.branch_office_income,
          branch_office_income_rate:
            res.data.pagination.count.branch_office_income_rate,
          consume_fee: res.data.pagination.count.consume_fee,
          consume_times: res.data.pagination.count.consume_times,
          consume_trade_amount: res.data.pagination.count.consume_trade_amount,
          income_consume_trade_amount:
            res.data.pagination.count.income_consume_trade_amount,
          level1_user_number: res.data.pagination.count.level1_user_number,
          level2_user_number: res.data.pagination.count.level2_user_number,
          level3_user_number: res.data.pagination.count.level3_user_number,
          realname_user_number: res.data.pagination.count.realname_user_number,
          refund_fee: res.data.pagination.count.refund_fee,
          refund_times: res.data.pagination.count.refund_times,
          refund_trade_amount: res.data.pagination.count.refund_trade_amount,
          repayment_fee: res.data.pagination.count.repayment_fee,
          repayment_times: res.data.pagination.count.repayment_times,
          repayment_trade_amount:
            res.data.pagination.count.repayment_trade_amount,
          user_number: res.data.pagination.count.user_number
        })
        this.pagination.total = res.data.pagination.total
      })
    },

    /**
     * 重置
     */
    handleReset() {
      this.page = 1
      this.pageSize = 20
      this.mobile = ''
      this.real_name = ''
      this.card_number = ''
      this.level = ''
      this.getData()
    },

    /**
     * 搜索
     */
    handleSearch() {
      // this.$router.replace({ query: '' })
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 100px;
  display: inline-block;
}
.w200 {
  width: 200px;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
